import pymysql


class DBTools:
    @classmethod
    def __get_connect(cls):
        conn = pymysql.connect(host='localhost', port=3306, user='root', password='root', database='class_db',
                               charset='utf8')
        return conn

    @classmethod
    def uid_db(cls, sql):
        conn = None
        cursor = None
        try:
            conn = cls.__get_connect()
            cursor = conn.cursor()
            cursor.execute(sql)
            conn.commit()
            print('受影响行数', conn.affected_rows())
        except Exception as a:
            print('出错了拉！！！', str(a))
            conn.rollback()
        finally:
            cursor.close()
            conn.close()

    @classmethod
    def select_one(cls, sql):
        cursor = None
        conn = None
        res = None
        try:
            conn = cls.__get_connect()
            cursor = conn.cursor()
            cursor.execute(sql)
            res = cursor.fetchone()

        except Exception as e:
            print('出错了拉！', e)
        finally:
            cursor.close()
            conn.close()
            return res


if __name__ == '__main__':
    sql = 'update student set money = 2000 where stu_id = 9'
    DBTools.uid_db(sql)
